home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / DirectSound / PlaySound / main.cs next >
Encoding:
Text File  |  2004-09-27  |  7.4 KB  |  224 lines

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.IO;
  5. using Microsoft.DirectX;
  6. using Microsoft.DirectX.DirectSound;
  7. using Buffer = Microsoft.DirectX.DirectSound.SecondaryBuffer;
  8. using Microsoft.Samples.DirectX.UtilityToolkit;
  9.  
  10.  
  11. public class MainForm : Form
  12. {
  13.     private System.ComponentModel.Container components = null;
  14.     private System.Windows.Forms.Button btnSoundfile;
  15.     private Label lblFilename;
  16.     private CheckBox cbLoopCheck;
  17.     private System.Windows.Forms.Button btnPlay;
  18.     private System.Windows.Forms.Button btnStop;
  19.     private System.Windows.Forms.Button btnCancel;
  20.         
  21.     private SecondaryBuffer ApplicationBuffer = null;
  22.     private Device ApplicationDevice = null;
  23.     private string PathSoundFile = string.Empty;
  24.  
  25.     public static void Main()
  26.     {
  27.         using (MainForm form = new MainForm())
  28.         {
  29.             Application.Run(form);
  30.         }
  31.     }
  32.     
  33.     protected override void Dispose( bool disposing )
  34.     {
  35.         if(disposing)
  36.         {
  37.             if (null != components)
  38.             {
  39.                 components.Dispose();
  40.             }
  41.         }
  42.         base.Dispose(disposing);    
  43.     }
  44.     public MainForm()
  45.     {
  46.         //
  47.         // Required for Windows Form Designer support
  48.         //
  49.         InitializeComponent();
  50.     }
  51.     #region InitializeComponent code
  52.     private void InitializeComponent()
  53.     {
  54.         this.btnSoundfile = new System.Windows.Forms.Button();
  55.         this.lblFilename = new System.Windows.Forms.Label();
  56.         this.cbLoopCheck = new System.Windows.Forms.CheckBox();
  57.         this.btnPlay = new System.Windows.Forms.Button();
  58.         this.btnStop = new System.Windows.Forms.Button();
  59.         this.btnCancel = new System.Windows.Forms.Button();
  60.         this.SuspendLayout();
  61.         // 
  62.         // btnSoundfile
  63.         // 
  64.         this.btnSoundfile.Location = new System.Drawing.Point(10, 11);
  65.         this.btnSoundfile.Name = "btnSoundfile";
  66.         this.btnSoundfile.Size = new System.Drawing.Size(69, 21);
  67.         this.btnSoundfile.TabIndex = 0;
  68.         this.btnSoundfile.Text = "Sound &file...";
  69.         this.btnSoundfile.Click += new System.EventHandler(this.btnSoundfile_Click);
  70.         // 
  71.         // lblFilename
  72.         // 
  73.         this.lblFilename.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  74.         this.lblFilename.Location = new System.Drawing.Point(94, 11);
  75.         this.lblFilename.Name = "lblFilename";
  76.         this.lblFilename.Size = new System.Drawing.Size(345, 21);
  77.         this.lblFilename.TabIndex = 1;
  78.         this.lblFilename.Text = "No file loaded.";
  79.         this.lblFilename.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  80.         // 
  81.         // cbLoopCheck
  82.         // 
  83.         this.cbLoopCheck.Enabled = false;
  84.         this.cbLoopCheck.Location = new System.Drawing.Point(9, 44);
  85.         this.cbLoopCheck.Name = "cbLoopCheck";
  86.         this.cbLoopCheck.Size = new System.Drawing.Size(87, 16);
  87.         this.cbLoopCheck.TabIndex = 2;
  88.         this.cbLoopCheck.Text = "&Loop sound";
  89.         // 
  90.         // btnPlay
  91.         // 
  92.         this.btnPlay.Enabled = false;
  93.         this.btnPlay.Location = new System.Drawing.Point(104, 48);
  94.         this.btnPlay.Name = "btnPlay";
  95.         this.btnPlay.TabIndex = 3;
  96.         this.btnPlay.Text = "&Play";
  97.         this.btnPlay.Click += new System.EventHandler(this.btnPlay_Click);
  98.         // 
  99.         // btnStop
  100.         // 
  101.         this.btnStop.Enabled = false;
  102.         this.btnStop.Location = new System.Drawing.Point(176, 48);
  103.         this.btnStop.Name = "btnStop";
  104.         this.btnStop.TabIndex = 4;
  105.         this.btnStop.Text = "&Stop";
  106.         this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
  107.         // 
  108.         // btnCancel
  109.         // 
  110.         this.btnCancel.Location = new System.Drawing.Point(364, 48);
  111.         this.btnCancel.Name = "btnCancel";
  112.         this.btnCancel.TabIndex = 5;
  113.         this.btnCancel.Text = "E&xit";
  114.         this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
  115.         // 
  116.         // MainForm
  117.         // 
  118.         this.AcceptButton = this.btnSoundfile;
  119.         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  120.         this.ClientSize = new System.Drawing.Size(450, 77);
  121.         this.Controls.AddRange(new System.Windows.Forms.Control[] {
  122.                                                                       this.btnSoundfile,
  123.                                                                       this.lblFilename,
  124.                                                                       this.cbLoopCheck,
  125.                                                                       this.btnPlay,
  126.                                                                       this.btnStop,
  127.                                                                       this.btnCancel});
  128.         this.Name = "MainForm";
  129.         this.Text = "PlaySound";
  130.         this.Load += new System.EventHandler(this.MainForm_Load);
  131.         this.ResumeLayout(false);
  132.  
  133.     }
  134.     #endregion
  135.  
  136.     private void btnCancel_Click(object sender, System.EventArgs e)
  137.     {
  138.         Close();
  139.     }
  140.  
  141.     private void btnStop_Click(object sender, System.EventArgs e)
  142.     {
  143.         if(null != ApplicationBuffer)
  144.             ApplicationBuffer.Stop();
  145.     }
  146.  
  147.     private void btnSoundfile_Click(object sender, System.EventArgs e)
  148.     {
  149.         OpenFileDialog ofd = new OpenFileDialog();
  150.  
  151.         if(string.Empty == PathSoundFile)
  152.                PathSoundFile = Utility.SdkMediaPath;
  153.  
  154.         ofd.InitialDirectory = PathSoundFile;
  155.         ofd.Filter=  "Wave files(*.wav)|*.wav";
  156.  
  157.         if( DialogResult.Cancel == ofd.ShowDialog() )
  158.             return;
  159.      
  160.         if(LoadSoundFile(ofd.FileName))
  161.         {
  162.             PathSoundFile = Path.GetDirectoryName(ofd.FileName);
  163.             lblFilename.Text = Path.GetFileName(ofd.FileName);
  164.             EnablePlayUI(true);
  165.         }
  166.         else
  167.         {
  168.             lblFilename.Text = "No file loaded.";
  169.             EnablePlayUI(false);
  170.         }
  171.     }
  172.  
  173.     private bool LoadSoundFile(string name)
  174.     {
  175.         try
  176.         {
  177.             ApplicationBuffer = new SecondaryBuffer(name, ApplicationDevice);
  178.         }
  179.         catch(SoundException)
  180.         {
  181.             return false;
  182.         }
  183.         return true;
  184.     }
  185.  
  186.     private void EnablePlayUI(bool enable)
  187.     {
  188.         if (enable)
  189.         {
  190.             cbLoopCheck.Enabled = true;
  191.             btnCancel.Enabled = true;
  192.             btnPlay.Enabled = true;
  193.             btnStop.Enabled = true;
  194.         }
  195.         else
  196.         {
  197.             cbLoopCheck.Enabled = false;
  198.             btnCancel.Enabled = false;
  199.             btnPlay.Enabled = false;
  200.             btnStop.Enabled = false;
  201.         }
  202.     }
  203.  
  204.     private void MainForm_Load(object sender, System.EventArgs e)
  205.     {
  206.         try
  207.         {
  208.             ApplicationDevice = new Device();
  209.             ApplicationDevice.SetCooperativeLevel(this, CooperativeLevel.Priority);
  210.         }
  211.         catch
  212.         {
  213.             MessageBox.Show("Unable to create sound device. Sample will now exit.");
  214.             this.Close();
  215.         }
  216.     }
  217.  
  218.     private void btnPlay_Click(object sender, System.EventArgs e)
  219.     {
  220.         if(null != ApplicationBuffer)
  221.             ApplicationBuffer.Play(0, (cbLoopCheck.Checked ? BufferPlayFlags.Looping : BufferPlayFlags.Default));
  222.     }
  223. }
  224.